<template>
  <div>
    <h1>商品管理</h1>

    <!-- 搜索 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input
          v-model="formInline.user"
          placeholder="根据用户名搜索"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="reslist"
      tooltip-effect="dark"
      style="width: 100%"
      border
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="nickname" label="用户名" width="120">
        <!-- <template slot-scope="scope">{{ scope.row.date }}</template> -->
      </el-table-column>
      <el-table-column prop="adress" label="地址"> </el-table-column>
      <el-table-column prop="avatar" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="100px" height="100px" />
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="修改日期">
        <template slot-scope="scope">{{ scope.row.created_at | filtime }}</template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="remove"> 删除 </el-button>
          <el-button type="success" size="small" @click="edit(scope.row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="list.length"
      :page-size="size"
      :page-sizes="[1, 2, 3, 4, 5]"
      :current-page="page"
      @current-change="change"
      @size-change="changes"
    >
    </el-pagination>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑框" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.adress" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="yes">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      addlist: [],
      size: 5,
      page: 1,
      dialogFormVisible: false,
      form: {
        nickname: "",
        adress: "",
      },
      formInline: {
        user: "",
      },
    };
  },
  mounted() {
    this.$axios.get("/list.json").then((res) => {
      this.list = res.data;
      this.addlist=res.data
    });
  },
  computed: {
    reslist() {
      return this.list.slice(
        this.size * (this.page - 1),
        this.size * this.page
      );
    },
  },
  methods: {
    change(val) {
      this.page = val;
    },
    changes(a) {
      this.size = a;
    },
    //删除
    remove(index) {
      this.list.splice(index, 1);
    },
    //编辑按钮
    edit(val) {
      this.dialogFormVisible = true;

      this.form.uid = val.uid;
      this.form.nickname = val.nickname;
      this.form.adress = val.adress;
    },
    //编辑的确定按钮
    yes() {
      this.dialogFormVisible = false;

      this.list.forEach((item) => {
        if (item.uid == this.form.uid) {
          item.uid = this.form.uid;
          item.nickname = this.form.nickname;
          item.adress = this.form.adress;
        }
      });
    },

    //查询
    search() {
      this.list = this.addlist.filter((item) => {
          return item.nickname.includes(this.formInline.user)    
        console.log(item);
      });
    },
  },

  filters:{
      filtime(val){
          return new Date(val*1000).toLocaleDateString()
      }
  }
};
</script>